package com.infindo.appcreate.zzyj.controller.mobi;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.infindo.appcreate.zzyj.dao.UserDao;
import com.infindo.appcreate.zzyj.entity.SysUser;
import com.infindo.appcreate.zzyj.service.UserService;
import com.infindo.appcreate.zzyj.util.RandomUtil;
import com.infindo.appcreate.zzyj.util.StringUtil;

@Controller
@RequestMapping(value = "/mobi/user/")
public class UserController {
	protected final Logger log = Logger.getLogger(getClass());
	
	@Resource
	UserService userService;

	@Resource
	UserDao userDao;
	
	@RequestMapping(value = "signon.jsonp")
	public Map<String, Object> signon(HttpServletRequest request,
			ModelMap modelMap) throws IOException {
		Map<String, Object> res = new HashMap<String, Object>();
		
		String account = request.getParameter("account");
		String password = request.getParameter("passwd");
		
		if (StringUtil.isEmpty(account) || StringUtil.isEmpty(password)) {
			res.put("success", false);
			return res;
		}

		DetachedCriteria dc = DetachedCriteria.forClass(SysUser.class);
		dc.add(Restrictions.eq("account", account));
		dc.add(Restrictions.eq("passwd", password));

		
		List<SysUser> ls = userDao.findByCriteria(dc);
		SysUser user = null;
		String token = null;
		if (ls.size() > 0) {
			user = ls.get(0);

			token = RandomUtil.RandomString();
			//user.setSignonToken(token);
			userDao.update(user);

			request.getSession().setAttribute("user", user);

			res.put("success", true);
			res.put("account", account);
			res.put("token", token);
			//res.put("guestToken", user.getGuestToken());
			return res;
		} else {
			res.put("success", false);
			return res;
		}		
	}

	@RequestMapping(value = "signonWithToken.jsonp")
	public Map<String, Object> signonWithToken(HttpServletRequest request,
			ModelMap modelMap) throws IOException {
		
		Map<String, Object> res = new HashMap<String, Object>();
		
		String token = request.getParameter("token");		
		if (StringUtil.isEmpty(token)) {
			res.put("success", false);
			return res;
		}

		DetachedCriteria dc = DetachedCriteria.forClass(SysUser.class);
		dc.add(Restrictions.eq("signonToken", token));

		List<SysUser> ls = userDao.findByCriteria(dc);
		SysUser user = null;
		if (ls.size() > 0) {
			user = ls.get(0);
			request.getSession().setAttribute("user", user);

			res.put("success", true);
			res.put("account", user.getAccount());
			res.put("token", token);
			//res.put("guestToken", user.getGuestToken());
			return res;
		} else {
			res.put("success", false);
			return res;
		}
	}
	
	@RequestMapping(value = "signonAsGuest.jsonp")
	public Map<String, Object> signonAsGuest(HttpServletRequest request,
			ModelMap modelMap) throws IOException {
		
		Map<String, Object> res = new HashMap<String, Object>();
		
		String token = request.getParameter("token");		
		if (StringUtil.isEmpty(token)) {
			res.put("success", false);
			return res;
		}

		DetachedCriteria dc = DetachedCriteria.forClass(SysUser.class);
		dc.add(Restrictions.eq("guestToken", token));

		List<SysUser> ls = userDao.findByCriteria(dc);
		SysUser user = null;
		if (ls.size() > 0) {
			user = ls.get(0);
			request.getSession().setAttribute("user", user);

			res.put("success", true);
			res.put("account", user.getAccount());
			//res.put("guestToken", user.getGuestToken());
			return res;
		} else {
			res.put("success", false);
			return res;
		}
	}

	@RequestMapping(value = "signup.jsonp")
	public Map<String, Object> signup(HttpServletRequest request,
			ModelMap modelMap) throws IOException {
		Map<String, Object> res = new HashMap<String, Object>();
		
		String account = request.getParameter("account");
		String password = request.getParameter("passwd");
		
		if (StringUtil.isEmpty(account) || StringUtil.isEmpty(password)) {
			res.put("success", false);
			return res;
		}
		
		DetachedCriteria dc = DetachedCriteria.forClass(SysUser.class);
		dc.add(Restrictions.eq("account", account));
		List<SysUser> ls = userDao.findByCriteria(dc);
		if (ls.size() > 0) {
			res.put("success", false);
			res.put("msg", "账户已存在");
			return res;
		}
		
		SysUser user = new SysUser();
		user.setAccount(account);
		user.setPasswd(password);					
		String token = RandomUtil.RandomString();
		String guestToken = RandomUtil.RandomString();
//		user.setSignonToken(token);
//		user.setGuestToken(guestToken);
		user.setStatus(1);
		user.setUpdateTime(new Date());
		userDao.save(user);
		
		request.getSession().setAttribute("user", user);
		
		res.put("success", true);
		res.put("account", account);
		res.put("token", token);
		res.put("guestToken", guestToken);
		return res;
	}

}
